#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sstream>
#include <iostream>
#include<stdlib.h>
#include "zhongdun_xa_msgFactory.h"
#include "zhongdun_xa_msgHandler.h"
#include "../../fep/xml_conf.h"
#include "../../fep/wrapperjson.h"
#include "../../fep/cmd.h"
#include "../../fep/buf_misc.h"
#include "msgcmn.h"

using namespace std;

static string recvZhongdunXaMsgBuf;
static U32  currentZhongdunXaMsgSize = 0;
static U32  liDunXaSequenceID = 1;
static U16  cmccLAC = 0;
static U16  unicomLAC = 0;
static U32  equipmentID = 0;
static U32  equipmentID1 = 0;
static bool startUpgrade = false;
static I32 sockType = 0;
static const char* x86Appfs = "/mnt/usr/appfs";
static const char* x86pkg = "/mnt/usr/appfs/pkg";
const U16 CRC16_Table[]= 
{ 
	0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 
	0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 
	0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 
	0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 
	0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 
	0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 
	0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 
	0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 
	0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 
	0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 
	0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 
	0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 
	0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 
	0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 
	0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 
	0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 
	0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 
	0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 
	0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 
	0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 
	0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 
	0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 
	0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 
	0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 
	0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 
	0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 
	0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 
	0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 
	0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 
	0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 
	0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 
	0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 
}; 

U16 crc_16(char *buf,int len) 
{ 
	U16 crc=0xffff; 
	while(len>0) 
	{ 
		crc=(crc>>8)^CRC16_Table[(crc^*buf)&0xff]; 
		len--; 
		buf++; 
	} 
	return ~crc;
}

void encBCD(const char* lpStr, int nLen, char *pBuf)
{
	int i,nStrLen;
	
	nStrLen=(int)strlen(lpStr);
	if(nStrLen>0)
	{
		//set to FF
		memset(pBuf,0xFF,nLen);
	}
	
	if( nStrLen > (nLen*2) )
	{	 
	  nStrLen=(nLen*2);
	}
	
	for(i=0;i<nStrLen;i++)
	{
		if(i==0)
		{
			pBuf[0] &= (lpStr[0]-'0')<<4;
			pBuf[0] &=0xF0; 
		}
		else
		{
			if(i%2 == 0)
			{
				pBuf[(i+1)/2] &= (((lpStr[i]-'0')<<4)|0x0F);
			}
			else
			{
				pBuf[(i+1)/2] &= ((lpStr[i]-'0')|0xF0);
			}
		}
	}
}

bool parse_MSG_START_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_START_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.equipmentId_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.equipmentId_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    uint16_t v16 = 0;
    if( msgSize < sizeof(msgRef.version_) )
    {
        return false;
    }

    memcpy(&v16, tmpPtr, sizeof(v16));
    msgRef.version_ = ntohs(v16);
    tmpPtr += sizeof(v16);
    msgSize -= sizeof(uint16_t);

    if( msgSize < sizeof(msgRef.date_) )
    {
        return false;
    }

    memcpy(&(msgRef.date_), tmpPtr, sizeof(msgRef.date_));
    tmpPtr += sizeof(msgRef.date_);
    msgSize -= sizeof(msgRef.date_);

    return true;
}

bool build_MSG_START_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_START_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.equipmentId_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    uint16_t v16 = 0;
    memcpy(&v16, &(msgRef.version_), sizeof(v16));
    v16 = htons(v16);
    memcpy(tmpPtr, &v16, sizeof(v16));
    tmpPtr += sizeof(v16);
    msgSize += sizeof(v16);

    memcpy(tmpPtr, &(msgRef.date_), sizeof(msgRef.date_));
    tmpPtr += sizeof(msgRef.date_);
    msgSize += sizeof(msgRef.date_);

    printf("build_MSG_START_REQ %d \n", msgSize);
    return true;
}

bool parse_MSG_START_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_START_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.result_) )
    {
        return false;
    }

    msgRef.result_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_START_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_START_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.result_;
    msgSize++;

    return true;
}

bool parse_MSG_CONFIG_REPORT_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_CONFIG_REPORT_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.equipmentId_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.equipmentId_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.frequency_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.frequency_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.power_) )
    {
        return false;
    }

    msgRef.power_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_CONFIG_REPORT_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_CONFIG_REPORT_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.equipmentId_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(&v32, &(msgRef.frequency_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    *tmpPtr++ = msgRef.power_;
    msgSize++;

    return true;
}

bool parse_MSG_CONFIG_REPORT_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_CONFIG_REPORT_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.result_) )
    {
        return false;
    }

    msgRef.result_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_CONFIG_REPORT_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_CONFIG_REPORT_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.result_;
    msgSize++;

    return true;
}

bool parse_MSG_SET_DATETIME_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_SET_DATETIME_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.date_) )
    {
        return false;
    }

    memcpy(&(msgRef.date_), tmpPtr, sizeof(msgRef.date_));
    tmpPtr += sizeof(msgRef.date_);
    msgSize -= sizeof(msgRef.date_);

    if( msgSize < sizeof(msgRef.time_) )
    {
        return false;
    }

    memcpy(&(msgRef.time_), tmpPtr, sizeof(msgRef.time_));
    tmpPtr += sizeof(msgRef.time_);
    msgSize -= sizeof(msgRef.time_);

    return true;
}

bool build_MSG_SET_DATETIME_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_SET_DATETIME_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    memcpy(tmpPtr, &(msgRef.date_), sizeof(msgRef.date_));
    tmpPtr += sizeof(msgRef.date_);
    msgSize += sizeof(msgRef.date_);

    memcpy(tmpPtr, &(msgRef.time_), sizeof(msgRef.time_));
    tmpPtr += sizeof(msgRef.time_);
    msgSize += sizeof(msgRef.time_);

    return true;
}

bool parse_MSG_SET_DATETIME_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SET_DATETIME_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.result_) )
    {
        return false;
    }

    msgRef.result_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_SET_DATETIME_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SET_DATETIME_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.result_;
    msgSize++;

    return true;
}

bool parse_MSG_SET_BTS_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_SET_BTS_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.frequency_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.frequency_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.power_) )
    {
        return false;
    }

    msgRef.power_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.rac_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.rac_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.lacUpdateCircle_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.lacUpdateCircle_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    return true;
}

bool build_MSG_SET_BTS_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_SET_BTS_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.frequency_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    *tmpPtr++ = msgRef.power_;
    msgSize++;

    memcpy(&v32, &(msgRef.rac_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(&v32, &(msgRef.lacUpdateCircle_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    return true;
}

bool parse_MSG_SET_BTS_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SET_BTS_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.result_) )
    {
        return false;
    }

    msgRef.result_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_SET_BTS_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SET_BTS_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.result_;
    msgSize++;

    return true;
}

bool parse_MSG_USER_REPORT_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_USER_REPORT_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.sequenceNumber_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.sequenceNumber_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.timestamp_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.timestamp_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.imsi_) )
    {
        return false;
    }

    memcpy(&(msgRef.imsi_), tmpPtr, sizeof(msgRef.imsi_));
    tmpPtr += sizeof(msgRef.imsi_);
    msgSize -= sizeof(msgRef.imsi_);

    if( msgSize < sizeof(msgRef.imei_) )
    {
        return false;
    }

    memcpy(&(msgRef.imei_), tmpPtr, sizeof(msgRef.imei_));
    tmpPtr += sizeof(msgRef.imei_);
    msgSize -= sizeof(msgRef.imei_);

    if( msgSize < sizeof(msgRef.tmsi_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.tmsi_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    uint16_t v16 = 0;
    if( msgSize < sizeof(msgRef.lac_) )
    {
        return false;
    }

    memcpy(&v16, tmpPtr, sizeof(v16));
    msgRef.lac_ = ntohs(v16);
    tmpPtr += sizeof(v16);
    msgSize -= sizeof(uint16_t);

    if( msgSize < sizeof(msgRef.equipmentID_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.equipmentID_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    return true;
}

bool build_MSG_USER_REPORT_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_USER_REPORT_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.sequenceNumber_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(&v32, &(msgRef.timestamp_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(tmpPtr, &(msgRef.imsi_), sizeof(msgRef.imsi_));
    tmpPtr += sizeof(msgRef.imsi_);
    msgSize += sizeof(msgRef.imsi_);

    memcpy(tmpPtr, &(msgRef.imei_), sizeof(msgRef.imei_));
    tmpPtr += sizeof(msgRef.imei_);
    msgSize += sizeof(msgRef.imei_);

    memcpy(&v32, &(msgRef.tmsi_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    uint16_t v16 = 0;
    memcpy(&v16, &(msgRef.lac_), sizeof(v16));
    v16 = htons(v16);
    memcpy(tmpPtr, &v16, sizeof(v16));
    tmpPtr += sizeof(v16);
    msgSize += sizeof(v16);

    memcpy(&v32, &(msgRef.equipmentID_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    return true;
}

bool parse_MSG_GET_HISTORY_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_GET_HISTORY_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.dateTime_) )
    {
        return false;
    }

    memcpy(&(msgRef.dateTime_), tmpPtr, sizeof(msgRef.dateTime_));
    tmpPtr += sizeof(msgRef.dateTime_);
    msgSize -= sizeof(msgRef.dateTime_);

    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.sequenceNumber_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.sequenceNumber_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    return true;
}

bool build_MSG_GET_HISTORY_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_GET_HISTORY_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    memcpy(tmpPtr, &(msgRef.dateTime_), sizeof(msgRef.dateTime_));
    tmpPtr += sizeof(msgRef.dateTime_);
    msgSize += sizeof(msgRef.dateTime_);

    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.sequenceNumber_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    return true;
}

bool parse_MSG_GET_HISTORY_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_GET_HISTORY_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.result_) )
    {
        return false;
    }

    msgRef.result_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.sequenceNumber_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.sequenceNumber_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.timestamp_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.timestamp_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.imsi_) )
    {
        return false;
    }

    memcpy(&(msgRef.imsi_), tmpPtr, sizeof(msgRef.imsi_));
    tmpPtr += sizeof(msgRef.imsi_);
    msgSize -= sizeof(msgRef.imsi_);

    if( msgSize < sizeof(msgRef.imei_) )
    {
        return false;
    }

    memcpy(&(msgRef.imei_), tmpPtr, sizeof(msgRef.imei_));
    tmpPtr += sizeof(msgRef.imei_);
    msgSize -= sizeof(msgRef.imei_);

    if( msgSize < sizeof(msgRef.tmsi_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.tmsi_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    uint16_t v16 = 0;
    if( msgSize < sizeof(msgRef.lac_) )
    {
        return false;
    }

    memcpy(&v16, tmpPtr, sizeof(v16));
    msgRef.lac_ = ntohs(v16);
    tmpPtr += sizeof(v16);
    msgSize -= sizeof(uint16_t);

    if( msgSize < sizeof(msgRef.equipmentID_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.equipmentID_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    return true;
}

bool build_MSG_GET_HISTORY_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_GET_HISTORY_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.result_;
    msgSize++;

    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.sequenceNumber_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(&v32, &(msgRef.timestamp_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(tmpPtr, &(msgRef.imsi_), sizeof(msgRef.imsi_));
    tmpPtr += sizeof(msgRef.imsi_);
    msgSize += sizeof(msgRef.imsi_);

    memcpy(tmpPtr, &(msgRef.imei_), sizeof(msgRef.imei_));
    tmpPtr += sizeof(msgRef.imei_);
    msgSize += sizeof(msgRef.imei_);

    memcpy(&v32, &(msgRef.tmsi_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    uint16_t v16 = 0;
    memcpy(&v16, &(msgRef.lac_), sizeof(v16));
    v16 = htons(v16);
    memcpy(tmpPtr, &v16, sizeof(v16));
    tmpPtr += sizeof(v16);
    msgSize += sizeof(v16);

    memcpy(&v32, &(msgRef.equipmentID_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    return true;
}

bool parse_MSG_HEARTBEAT_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_HEARTBEAT_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.btsid_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.btsid_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    return true;
}

bool build_MSG_HEARTBEAT_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_HEARTBEAT_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.btsid_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    return true;
}

bool parse_MSG_WARNING_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_WARNING_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.warningType_) )
    {
        return false;
    }

    msgRef.warningType_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_WARNING_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_WARNING_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.warningType_;
    msgSize++;

    return true;
}

bool parse_MSG_RESET_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_RESET_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool build_MSG_RESET_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_RESET_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool parse_MSG_RESET_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_RESET_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool build_MSG_RESET_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_RESET_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool parse_MSG_SET_CONTROL_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_SET_CONTROL_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.mode_) )
    {
        return false;
    }

    msgRef.mode_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    uint16_t v16 = 0;
    if( msgSize < sizeof(msgRef.count_) )
    {
        return false;
    }

    memcpy(&v16, tmpPtr, sizeof(v16));
    msgRef.count_ = ntohs(v16);
    tmpPtr += sizeof(v16);
    msgSize -= sizeof(uint16_t);

    if( msgSize < sizeof(msgRef.number_) )
    {
        return false;
    }

    memcpy(&v16, tmpPtr, sizeof(v16));
    msgRef.number_ = ntohs(v16);
    tmpPtr += sizeof(v16);
    msgSize -= sizeof(uint16_t);

    if( msgSize < sizeof(msgRef.locateType_) )
    {
        return false;
    }

    msgRef.locateType_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.imsiOrImei_) )
    {
        return false;
    }

    memcpy(&(msgRef.imsiOrImei_), tmpPtr, sizeof(msgRef.imsiOrImei_));
    tmpPtr += sizeof(msgRef.imsiOrImei_);
    msgSize -= sizeof(msgRef.imsiOrImei_);

    return true;
}

bool build_MSG_SET_CONTROL_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_SET_CONTROL_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.mode_;
    msgSize++;

    uint16_t v16 = 0;
    memcpy(&v16, &(msgRef.count_), sizeof(v16));
    v16 = htons(v16);
    memcpy(tmpPtr, &v16, sizeof(v16));
    tmpPtr += sizeof(v16);
    msgSize += sizeof(v16);

    memcpy(&v16, &(msgRef.number_), sizeof(v16));
    v16 = htons(v16);
    memcpy(tmpPtr, &v16, sizeof(v16));
    tmpPtr += sizeof(v16);
    msgSize += sizeof(v16);

    *tmpPtr++ = msgRef.locateType_;
    msgSize++;

    memcpy(tmpPtr, &(msgRef.imsiOrImei_), sizeof(msgRef.imsiOrImei_));
    tmpPtr += sizeof(msgRef.imsiOrImei_);
    msgSize += sizeof(msgRef.imsiOrImei_);

    return true;
}

bool parse_MSG_SET_CONTROL_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SET_CONTROL_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.mode_) )
    {
        return false;
    }

    msgRef.mode_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.result_) )
    {
        return false;
    }

    msgRef.result_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_SET_CONTROL_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SET_CONTROL_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.mode_;
    msgSize++;

    *tmpPtr++ = msgRef.result_;
    msgSize++;

    return true;
}

bool parse_MSG_CONTROL_OPTION_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_CONTROL_OPTION_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.mode_) )
    {
        return false;
    }

    msgRef.mode_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.submode_) )
    {
        return false;
    }

    msgRef.submode_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.locateType_) )
    {
        return false;
    }

    msgRef.locateType_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.imsiOrImei) )
    {
        return false;
    }

    memcpy(&(msgRef.imsiOrImei), tmpPtr, sizeof(msgRef.imsiOrImei));
    tmpPtr += sizeof(msgRef.imsiOrImei);
    msgSize -= sizeof(msgRef.imsiOrImei);

    if( msgSize < sizeof(msgRef.moNumber_) )
    {
        return false;
    }

    memcpy(&(msgRef.moNumber_), tmpPtr, sizeof(msgRef.moNumber_));
    tmpPtr += sizeof(msgRef.moNumber_);
    msgSize -= sizeof(msgRef.moNumber_);

    if( msgSize < sizeof(msgRef.info_) )
    {
        return false;
    }

    memcpy(&(msgRef.info_), tmpPtr, sizeof(msgRef.info_));
    tmpPtr += sizeof(msgRef.info_);
    msgSize -= sizeof(msgRef.info_);

    return true;
}

bool build_MSG_CONTROL_OPTION_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_CONTROL_OPTION_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.mode_;
    msgSize++;

    *tmpPtr++ = msgRef.submode_;
    msgSize++;

    *tmpPtr++ = msgRef.locateType_;
    msgSize++;

    memcpy(tmpPtr, &(msgRef.imsiOrImei), sizeof(msgRef.imsiOrImei));
    tmpPtr += sizeof(msgRef.imsiOrImei);
    msgSize += sizeof(msgRef.imsiOrImei);

    memcpy(tmpPtr, &(msgRef.moNumber_), sizeof(msgRef.moNumber_));
    tmpPtr += sizeof(msgRef.moNumber_);
    msgSize += sizeof(msgRef.moNumber_);

    memcpy(tmpPtr, &(msgRef.info_), sizeof(msgRef.info_));
    tmpPtr += sizeof(msgRef.info_);
    msgSize += sizeof(msgRef.info_);

    return true;
}

bool parse_MSG_CONTROL_OPTION_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_CONTROL_OPTION_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.mode_) )
    {
        return false;
    }

    msgRef.mode_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.subMode_) )
    {
        return false;
    }

    msgRef.subMode_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.result_) )
    {
        return false;
    }

    msgRef.result_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_CONTROL_OPTION_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_CONTROL_OPTION_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.mode_;
    msgSize++;

    *tmpPtr++ = msgRef.subMode_;
    msgSize++;

    *tmpPtr++ = msgRef.result_;
    msgSize++;

    return true;
}

bool parse_MSG_RSSI_REPORT_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_RSSI_REPORT_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.timeStamp_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.timeStamp_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.imsi_) )
    {
        return false;
    }

    memcpy(&(msgRef.imsi_), tmpPtr, sizeof(msgRef.imsi_));
    tmpPtr += sizeof(msgRef.imsi_);
    msgSize -= sizeof(msgRef.imsi_);

    if( msgSize < sizeof(msgRef.imei_) )
    {
        return false;
    }

    memcpy(&(msgRef.imei_), tmpPtr, sizeof(msgRef.imei_));
    tmpPtr += sizeof(msgRef.imei_);
    msgSize -= sizeof(msgRef.imei_);

    if( msgSize < sizeof(msgRef.equipment_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.equipment_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.rssi_) )
    {
        return false;
    }

    msgRef.rssi_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_RSSI_REPORT_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_RSSI_REPORT_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.timeStamp_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(tmpPtr, &(msgRef.imsi_), sizeof(msgRef.imsi_));
    tmpPtr += sizeof(msgRef.imsi_);
    msgSize += sizeof(msgRef.imsi_);

    memcpy(tmpPtr, &(msgRef.imei_), sizeof(msgRef.imei_));
    tmpPtr += sizeof(msgRef.imei_);
    msgSize += sizeof(msgRef.imei_);

    memcpy(&v32, &(msgRef.equipment_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    *tmpPtr++ = msgRef.rssi_;
    msgSize++;

    return true;
}

bool parse_MSG_SMS_RECV_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_SMS_RECV_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.timeStamp_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.timeStamp_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.imsi_) )
    {
        return false;
    }

    memcpy(&(msgRef.imsi_), tmpPtr, sizeof(msgRef.imsi_));
    tmpPtr += sizeof(msgRef.imsi_);
    msgSize -= sizeof(msgRef.imsi_);

    if( msgSize < sizeof(msgRef.imei_) )
    {
        return false;
    }

    memcpy(&(msgRef.imei_), tmpPtr, sizeof(msgRef.imei_));
    tmpPtr += sizeof(msgRef.imei_);
    msgSize -= sizeof(msgRef.imei_);

    if( msgSize < sizeof(msgRef.number_) )
    {
        return false;
    }

    memcpy(&(msgRef.number_), tmpPtr, sizeof(msgRef.number_));
    tmpPtr += sizeof(msgRef.number_);
    msgSize -= sizeof(msgRef.number_);

    if( msgSize < sizeof(msgRef.content_) )
    {
        return false;
    }

    memcpy(&(msgRef.content_), tmpPtr, sizeof(msgRef.content_));
    tmpPtr += sizeof(msgRef.content_);
    msgSize -= sizeof(msgRef.content_);

    return true;
}

bool build_MSG_SMS_RECV_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_SMS_RECV_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.timeStamp_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(tmpPtr, &(msgRef.imsi_), sizeof(msgRef.imsi_));
    tmpPtr += sizeof(msgRef.imsi_);
    msgSize += sizeof(msgRef.imsi_);

    memcpy(tmpPtr, &(msgRef.imei_), sizeof(msgRef.imei_));
    tmpPtr += sizeof(msgRef.imei_);
    msgSize += sizeof(msgRef.imei_);

    memcpy(tmpPtr, &(msgRef.number_), sizeof(msgRef.number_));
    tmpPtr += sizeof(msgRef.number_);
    msgSize += sizeof(msgRef.number_);

    memcpy(tmpPtr, &(msgRef.content_), sizeof(msgRef.content_));
    tmpPtr += sizeof(msgRef.content_);
    msgSize += sizeof(msgRef.content_);

    return true;
}

bool parse_MSG_CALL_RECV_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_CALL_RECV_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.timeStamp_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.timeStamp_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.imsi_) )
    {
        return false;
    }

    memcpy(&(msgRef.imsi_), tmpPtr, sizeof(msgRef.imsi_));
    tmpPtr += sizeof(msgRef.imsi_);
    msgSize -= sizeof(msgRef.imsi_);

    if( msgSize < sizeof(msgRef.imei_) )
    {
        return false;
    }

    memcpy(&(msgRef.imei_), tmpPtr, sizeof(msgRef.imei_));
    tmpPtr += sizeof(msgRef.imei_);
    msgSize -= sizeof(msgRef.imei_);

    if( msgSize < sizeof(msgRef.number_) )
    {
        return false;
    }

    memcpy(&(msgRef.number_), tmpPtr, sizeof(msgRef.number_));
    tmpPtr += sizeof(msgRef.number_);
    msgSize -= sizeof(msgRef.number_);

    return true;
}

bool build_MSG_CALL_RECV_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_CALL_RECV_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.timeStamp_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(tmpPtr, &(msgRef.imsi_), sizeof(msgRef.imsi_));
    tmpPtr += sizeof(msgRef.imsi_);
    msgSize += sizeof(msgRef.imsi_);

    memcpy(tmpPtr, &(msgRef.imei_), sizeof(msgRef.imei_));
    tmpPtr += sizeof(msgRef.imei_);
    msgSize += sizeof(msgRef.imei_);

    memcpy(tmpPtr, &(msgRef.number_), sizeof(msgRef.number_));
    tmpPtr += sizeof(msgRef.number_);
    msgSize += sizeof(msgRef.number_);

    return true;
}

bool parse_MSG_GET_VERSION(uint8_t* msgData, uint32_t& msgSize, MSG_GET_VERSION& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool build_MSG_GET_VERSION(uint8_t* msgData, uint32_t& msgSize, MSG_GET_VERSION& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool parse_MSG_GET_VERSION_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_GET_VERSION_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.version_) )
    {
        return false;
    }

    memcpy(&(msgRef.version_), tmpPtr, sizeof(msgRef.version_));
    tmpPtr += sizeof(msgRef.version_);
    msgSize -= sizeof(msgRef.version_);

    if( msgSize < sizeof(msgRef.date_) )
    {
        return false;
    }

    memcpy(&(msgRef.date_), tmpPtr, sizeof(msgRef.date_));
    tmpPtr += sizeof(msgRef.date_);
    msgSize -= sizeof(msgRef.date_);

    return true;
}

bool build_MSG_GET_VERSION_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_GET_VERSION_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    memcpy(tmpPtr, &(msgRef.version_), sizeof(msgRef.version_));
    tmpPtr += sizeof(msgRef.version_);
    msgSize += sizeof(msgRef.version_);

    memcpy(tmpPtr, &(msgRef.date_), sizeof(msgRef.date_));
    tmpPtr += sizeof(msgRef.date_);
    msgSize += sizeof(msgRef.date_);

    return true;
}

bool parse_MSG_SEND_PACKET(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_PACKET& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint16_t v16 = 0;
    if( msgSize < sizeof(msgRef.sn_) )
    {
        return false;
    }

    memcpy(&v16, tmpPtr, sizeof(v16));
    msgRef.sn_ = ntohs(v16);
    tmpPtr += sizeof(v16);
    msgSize -= sizeof(uint16_t);

    if( msgSize < sizeof(msgRef.length_) )
    {
        return false;
    }

    msgRef.length_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.data_) )
    {
        return false;
    }

    memcpy(&(msgRef.data_), tmpPtr, sizeof(msgRef.data_));
    tmpPtr += sizeof(msgRef.data_);
    msgSize -= sizeof(msgRef.data_);

    if( msgSize < sizeof(msgRef.crc_) )
    {
        return false;
    }

    memcpy(&(msgRef.crc_), tmpPtr, sizeof(msgRef.crc_));
    tmpPtr += sizeof(msgRef.crc_);
    msgSize -= sizeof(msgRef.crc_);

    return true;
}

bool build_MSG_SEND_PACKET(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_PACKET& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint16_t v16 = 0;
    memcpy(&v16, &(msgRef.sn_), sizeof(v16));
    v16 = htons(v16);
    memcpy(tmpPtr, &v16, sizeof(v16));
    tmpPtr += sizeof(v16);
    msgSize += sizeof(v16);

    *tmpPtr++ = msgRef.length_;
    msgSize++;

    memcpy(tmpPtr, &(msgRef.data_), sizeof(msgRef.data_));
    tmpPtr += sizeof(msgRef.data_);
    msgSize += sizeof(msgRef.data_);

    memcpy(tmpPtr, &(msgRef.crc_), sizeof(msgRef.crc_));
    tmpPtr += sizeof(msgRef.crc_);
    msgSize += sizeof(msgRef.crc_);

    return true;
}

bool parse_MSG_SEND_PACKET_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_PACKET_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.result_) )
    {
        return false;
    }

    msgRef.result_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_SEND_PACKET_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_PACKET_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.result_;
    msgSize++;

    return true;
}

bool parse_MSG_DOWNLOAD_END(uint8_t* msgData, uint32_t& msgSize, MSG_DOWNLOAD_END& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool build_MSG_DOWNLOAD_END(uint8_t* msgData, uint32_t& msgSize, MSG_DOWNLOAD_END& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool parse_MSG_GET_HISTORY_INFOMATION_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_GET_HISTORY_INFOMATION_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool build_MSG_GET_HISTORY_INFOMATION_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_GET_HISTORY_INFOMATION_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool parse_HISTORY_INFO_RECORD(uint8_t* msgData, uint32_t& msgSize, HISTORY_INFO_RECORD& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.dataTime_) )
    {
        return false;
    }

    memcpy(&(msgRef.dataTime_), tmpPtr, sizeof(msgRef.dataTime_));
    tmpPtr += sizeof(msgRef.dataTime_);
    msgSize -= sizeof(msgRef.dataTime_);

    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.sequence_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.sequence_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    return true;
}

bool build_HISTORY_INFO_RECORD(uint8_t* msgData, uint32_t& msgSize, HISTORY_INFO_RECORD& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    memcpy(tmpPtr, &(msgRef.dataTime_), sizeof(msgRef.dataTime_));
    tmpPtr += sizeof(msgRef.dataTime_);
    msgSize += sizeof(msgRef.dataTime_);

    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.sequence_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    return true;
}

bool parse_MSG_GET_HISTORY_INFOMATION_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_GET_HISTORY_INFOMATION_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t leftMsgSize = msgSize;
    while( leftMsgSize>0 )
    {
        HISTORY_INFO_RECORD record;
        if( parse_HISTORY_INFO_RECORD(tmpPtr, leftMsgSize, record) )
        {
            msgRef.hisInfoRecords_.push_back(record);
            tmpPtr += (msgSize - leftMsgSize);
            msgSize = leftMsgSize;
        }
        else
        {
            break;
        }
    }

    return true;
}

bool build_MSG_GET_HISTORY_INFOMATION_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_GET_HISTORY_INFOMATION_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    for( size_t i=0; i<msgRef.hisInfoRecords_.size(); i++ )
    {
        uint32_t tmpMsgSize = 0;
        if( !build_HISTORY_INFO_RECORD(tmpPtr, tmpMsgSize, msgRef.hisInfoRecords_[i]) )
        {
            return false;
        }
        tmpPtr += tmpMsgSize;
        msgSize += tmpMsgSize;
    }

    return true;
}

bool parse_MSG_SEND_RECORD_SELECT(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_RECORD_SELECT& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.date_) )
    {
        return false;
    }

    memcpy(&(msgRef.date_), tmpPtr, sizeof(msgRef.date_));
    tmpPtr += sizeof(msgRef.date_);
    msgSize -= sizeof(msgRef.date_);

    return true;
}

bool build_MSG_SEND_RECORD_SELECT(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_RECORD_SELECT& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    memcpy(tmpPtr, &(msgRef.date_), sizeof(msgRef.date_));
    tmpPtr += sizeof(msgRef.date_);
    msgSize += sizeof(msgRef.date_);

    return true;
}

bool parse_MSG_SEND_RECORD_SELECT_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_RECORD_SELECT_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.result_) )
    {
        return false;
    }

    msgRef.result_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.fileSize_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.fileSize_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    return true;
}

bool build_MSG_SEND_RECORD_SELECT_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_RECORD_SELECT_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.result_;
    msgSize++;

    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.fileSize_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    return true;
}

bool parse_MSG_SEND_RECORD_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_RECORD_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint16_t v16 = 0;
    if( msgSize < sizeof(msgRef.sn_) )
    {
        return false;
    }

    memcpy(&v16, tmpPtr, sizeof(v16));
    msgRef.sn_ = ntohs(v16);
    tmpPtr += sizeof(v16);
    msgSize -= sizeof(uint16_t);

    return true;
}

bool build_MSG_SEND_RECORD_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_RECORD_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint16_t v16 = 0;
    memcpy(&v16, &(msgRef.sn_), sizeof(v16));
    v16 = htons(v16);
    memcpy(tmpPtr, &v16, sizeof(v16));
    tmpPtr += sizeof(v16);
    msgSize += sizeof(v16);

    return true;
}

bool parse_MSG_SEND_RECORD_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_RECORD_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint16_t v16 = 0;
    if( msgSize < sizeof(msgRef.sn_) )
    {
        return false;
    }

    memcpy(&v16, tmpPtr, sizeof(v16));
    msgRef.sn_ = ntohs(v16);
    tmpPtr += sizeof(v16);
    msgSize -= sizeof(uint16_t);

    if( msgSize < sizeof(msgRef.num_) )
    {
        return false;
    }

    msgRef.num_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.data_) )
    {
        return false;
    }

    memcpy(&(msgRef.data_), tmpPtr, sizeof(msgRef.data_));
    tmpPtr += sizeof(msgRef.data_);
    msgSize -= sizeof(msgRef.data_);

    if( msgSize < sizeof(msgRef.crc_) )
    {
        return false;
    }

    memcpy(&(msgRef.crc_), tmpPtr, sizeof(msgRef.crc_));
    tmpPtr += sizeof(msgRef.crc_);
    msgSize -= sizeof(msgRef.crc_);

    return true;
}

bool build_MSG_SEND_RECORD_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_RECORD_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint16_t v16 = 0;
    memcpy(&v16, &(msgRef.sn_), sizeof(v16));
    v16 = htons(v16);
    memcpy(tmpPtr, &v16, sizeof(v16));
    tmpPtr += sizeof(v16);
    msgSize += sizeof(v16);

    *tmpPtr++ = msgRef.num_;
    msgSize++;

    memcpy(tmpPtr, &(msgRef.data_), sizeof(msgRef.data_));
    tmpPtr += sizeof(msgRef.data_);
    msgSize += sizeof(msgRef.data_);

    memcpy(tmpPtr, &(msgRef.crc_), sizeof(msgRef.crc_));
    tmpPtr += sizeof(msgRef.crc_);
    msgSize += sizeof(msgRef.crc_);

    return true;
}

bool parse_MSG_SEND_RECORD_END(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_RECORD_END& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool build_MSG_SEND_RECORD_END(uint8_t* msgData, uint32_t& msgSize, MSG_SEND_RECORD_END& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool parse_MSG_GET_BTS_INFO_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_GET_BTS_INFO_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool build_MSG_GET_BTS_INFO_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_GET_BTS_INFO_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool parse_MSG_GET_BTS_INFO_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_GET_BTS_INFO_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.arfcn_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.arfcn_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.power_) )
    {
        return false;
    }

    msgRef.power_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.rac_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.rac_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.luc_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.luc_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    return true;
}

bool build_MSG_GET_BTS_INFO_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_GET_BTS_INFO_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    int32_t i32 = 146;
    memcpy(&v32, &(msgRef.arfcn_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    *tmpPtr++ = msgRef.power_;
    msgSize++;

    i32 = htonl(146);
    cli_app_log(CLI_LOG_INFO, "gsm rac is %d \n", i32);
    cli_app_log(CLI_LOG_INFO, "gsm rac is %d \n", ntohl(i32));
    memcpy(tmpPtr, &i32, sizeof(i32));
    tmpPtr += sizeof(i32);
    msgSize += sizeof(i32);

    memcpy(&v32, &(msgRef.luc_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    return true;
}

bool parse_MSG_GET_TIME_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_GET_TIME_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool build_MSG_GET_TIME_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_GET_TIME_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool parse_MSG_GET_TIME_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_GET_TIME_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.timeStame_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.timeStame_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    return true;
}

bool build_MSG_GET_TIME_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_GET_TIME_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.timeStame_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    return true;
}

bool parse_MSG_DELETE_USERDATA_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_DELETE_USERDATA_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool build_MSG_DELETE_USERDATA_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_DELETE_USERDATA_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool parse_MSG_DELETE_USERDATA_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_DELETE_USERDATA_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.result_) )
    {
        return false;
    }

    msgRef.result_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_DELETE_USERDATA_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_DELETE_USERDATA_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.result_;
    msgSize++;

    return true;
}

bool parse_MSG_RF_POWER_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_RF_POWER_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    if( msgSize < sizeof(msgRef.onOfOff_) )
    {
        return false;
    }

    msgRef.onOfOff_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_RF_POWER_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_RF_POWER_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    *tmpPtr++ = msgRef.onOfOff_;
    msgSize++;

    return true;
}

bool parse_MSG_RF_POWER_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_RF_POWER_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}

bool build_MSG_RF_POWER_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_RF_POWER_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    return true;
}



bool parse_MSG_SCAN_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_SCAN_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.equipmentId_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.equipmentId_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.operatorsId_) )
    {
        return false;
    }

    msgRef.operatorsId_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_SCAN_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_SCAN_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.equipmentId_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    *tmpPtr++ = msgRef.operatorsId_;
    msgSize++;

    return true;
}

bool parse_SCAN_NEIGH_INFO(uint8_t* msgData, uint32_t& msgSize, SCAN_NEIGH_INFO& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
 


    if( msgSize < sizeof(msgRef.neighbourFrequency_) )
    {
        return false;
    }
    uint32_t v32 = 0;
    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.neighbourFrequency_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    return true;
}

bool build_SCAN_NEIGH_INFO(uint8_t* msgData, uint32_t& msgSize, SCAN_NEIGH_INFO& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;

    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.neighbourFrequency_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    return true;
}

bool parse_SCAN_BEST_FREQ_INFO(uint8_t* msgData, uint32_t& msgSize, SCAN_BEST_FREQ_INFO& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;

    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.bestFrequency_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.bestFrequency_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    return true;
}

bool build_SCAN_BEST_FREQ_INFO(uint8_t* msgData, uint32_t& msgSize, SCAN_BEST_FREQ_INFO& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
 
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.bestFrequency_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    return true;
}

bool parse_MSG_SCAN_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SCAN_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    for(uint32_t i=0; i<6; i++)
    {
        uint32_t leftMsgSize = msgSize;
        if( !parse_SCAN_NEIGH_INFO(tmpPtr, leftMsgSize, msgRef.neighInfo_[i]) )
        {
            return false;
        }
        tmpPtr += (msgSize - leftMsgSize);
        msgSize = leftMsgSize;
    }

    for(uint32_t i=0; i<6; i++)
    {
        uint32_t leftMsgSize = msgSize;
        if( !parse_SCAN_BEST_FREQ_INFO(tmpPtr, leftMsgSize, msgRef.bestFreqInfo_[i]) )
        {
            return false;
        }
        tmpPtr += (msgSize - leftMsgSize);
        msgSize = leftMsgSize;
    }

    return true;
}

bool build_MSG_SCAN_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_SCAN_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.equipmentId_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    *tmpPtr++ = msgRef.neighbourFrequencyTotle_;
    msgSize++;

    for(uint32_t i=0; i<6; i++)
    {
        uint32_t tmpMsgSize = 0;
        if( !build_SCAN_NEIGH_INFO(tmpPtr, tmpMsgSize, msgRef.neighInfo_[i]) )
        {
            return false;
        }
        tmpPtr += tmpMsgSize;
        msgSize += tmpMsgSize;
    }
   *tmpPtr++ = msgRef.bestFrequencyTotle_;
    msgSize++;

    for(uint32_t i=0; i<6; i++)
    {
        uint32_t tmpMsgSize = 0;
        if( !build_SCAN_BEST_FREQ_INFO(tmpPtr, tmpMsgSize, msgRef.bestFreqInfo_[i]) )
        {
            return false;
        }
        tmpPtr += tmpMsgSize;
        msgSize += tmpMsgSize;
    }

    return true;
}

bool parse_XML_SCAN_BCCH_INFO(uint8_t* msgData, uint32_t& msgSize, XML_SCAN_BCCH_INFO& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint16_t v16 = 0;
    if( msgSize < sizeof(msgRef.frequency_) )
    {
        return false;
    }

    memcpy(&v16, tmpPtr, sizeof(v16));
    msgRef.frequency_ = ntohs(v16);
    tmpPtr += sizeof(v16);
    msgSize -= sizeof(uint16_t);

    if( msgSize < sizeof(msgRef.rssi_) )
    {
        return false;
    }

    msgRef.rssi_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.agent_) )
    {
        return false;
    }

    msgRef.agent_ = *tmpPtr++;
    msgSize -= sizeof(uint8_t);

    if( msgSize < sizeof(msgRef.nebFreqTotle_) )
    {
        return false;
    }

    msgRef.nebFreqTotle_ = *tmpPtr++;
    msgSize -= sizeof(uint8_t);

    if( msgSize < sizeof(msgRef.nebFreq_) )
    {
        return false;
    }
    for(uint32_t i=0; i<96; i++)
    {
        memcpy(&v16, tmpPtr, sizeof(v16));
        msgRef.nebFreq_[i] = ntohs(v16);
        tmpPtr += sizeof(v16);
        msgSize -= sizeof(uint16_t);

    }

    return true;
}

bool build_XML_SCAN_BCCH_INFO(uint8_t* msgData, uint32_t& msgSize, XML_SCAN_BCCH_INFO& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint16_t v16 = 0;
    memcpy(&v16, &(msgRef.frequency_), sizeof(v16));
    v16 = htons(v16);
    memcpy(tmpPtr, &v16, sizeof(v16));
    tmpPtr += sizeof(v16);
    msgSize += sizeof(v16);

    *tmpPtr++ = msgRef.rssi_;
    msgSize++;

    *tmpPtr++ = msgRef.agent_;
    msgSize++;

    *tmpPtr++ = msgRef.nebFreqTotle_;
    msgSize++;

    for(uint32_t i=0; i<96; i++)
    {
        memcpy(&v16, &(msgRef.nebFreq_[i]), sizeof(v16));
        v16 = htons(v16);
        memcpy(tmpPtr, &v16, sizeof(v16));
        tmpPtr += sizeof(v16);
        msgSize += sizeof(v16);

    }

    return true;
}

bool parse_MSG_XML_SCAN_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_XML_SCAN_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.equipmentID1_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.equipmentID1_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.equipmentID2_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.equipmentID2_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.eqNum_) )
    {
        return false;
    }

    msgRef.eqNum_ = *tmpPtr++;
    msgSize -= sizeof(uint8_t);

    if( msgSize < sizeof(msgRef.result_) )
    {
        return false;
    }

    msgRef.result_ = *tmpPtr++;
    msgSize -= sizeof(uint8_t);

    if( msgSize < sizeof(msgRef.endSet_) )
    {
        return false;
    }

    msgRef.endSet_ = *tmpPtr++;
    msgSize -= sizeof(uint8_t);

    if( msgSize < sizeof(msgRef.packNum_) )
    {
        return false;
    }

    msgRef.packNum_ = *tmpPtr++;
    msgSize -= sizeof(uint8_t);

    if( msgSize < sizeof(msgRef.bcchCnt_) )
    {
        return false;
    }

    msgRef.bcchCnt_ = *tmpPtr++;
    msgSize -= sizeof(uint8_t);

    uint32_t leftMsgSize = msgSize;
    for(uint32_t i=0; i<msgRef.bcchCnt_; i++)
    {
        XML_SCAN_BCCH_INFO record;
        if( parse_XML_SCAN_BCCH_INFO(tmpPtr, leftMsgSize, record) )
        {
            msgRef.bcchRecord_.push_back(record);
            tmpPtr += (msgSize - leftMsgSize);
            msgSize = leftMsgSize;
        }
        else
        {
            return false;
        }
    }

    return true;
}

bool build_MSG_XML_SCAN_ACK(uint8_t* msgData, uint32_t& msgSize, MSG_XML_SCAN_ACK& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.equipmentID1_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(&v32, &(msgRef.equipmentID2_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    *tmpPtr++ = msgRef.eqNum_;
    msgSize++;

    *tmpPtr++ = msgRef.result_;
    msgSize++;

    *tmpPtr++ = msgRef.endSet_;
    msgSize++;

    *tmpPtr++ = msgRef.packNum_;
    msgSize++;

    *tmpPtr++ = msgRef.bcchCnt_;
    msgSize++;

    for( size_t i=0; i<msgRef.bcchRecord_.size(); i++ )
    {
        uint32_t tmpMsgSize = 0;
        if( !build_XML_SCAN_BCCH_INFO(tmpPtr, tmpMsgSize, msgRef.bcchRecord_[i]) )
        {
            return false;
        }
        tmpPtr += tmpMsgSize;
        msgSize += tmpMsgSize;
    }

    return true;
}

bool parse_MSG_GPSINFO_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_GPSINFO_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    if( msgSize < sizeof(msgRef.btsId_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.btsId_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.cpuUtil_) )
    {
        return false;
    }

    msgRef.cpuUtil_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.memUtil_) )
    {
        return false;
    }

    msgRef.memUtil_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.sdUtil_) )
    {
        return false;
    }

    msgRef.sdUtil_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.temp_) )
    {
        return false;
    }

    msgRef.temp_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.powerStat_) )
    {
        return false;
    }

    msgRef.powerStat_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.g3CardStat_) )
    {
        return false;
    }

    msgRef.g3CardStat_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.wifiStat_) )
    {
        return false;
    }

    msgRef.wifiStat_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.paSata) )
    {
        return false;
    }

    msgRef.paSata = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.att_) )
    {
        return false;
    }

    msgRef.att_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.paOut_) )
    {
        return false;
    }

    msgRef.paOut_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.uiYear_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.uiYear_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.uiMounth_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.uiMounth_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.uiDayOfWeek) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.uiDayOfWeek = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.uiDay_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.uiDay_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.uiHour_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.uiHour_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.uiMinute_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.uiMinute_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.uiSecond_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.uiSecond_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.uiMilliseconds_) )
    {
        return false;
    }

    memcpy(&v32, tmpPtr, sizeof(v32));
    msgRef.uiMilliseconds_ = ntohl(v32);
    tmpPtr += sizeof(v32);
    msgSize -= sizeof(uint32_t);

    if( msgSize < sizeof(msgRef.status_) )
    {
        return false;
    }

    msgRef.status_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.lat_) )
    {
        return false;
    }

    memcpy(&(msgRef.lat_), tmpPtr, sizeof(double));
    tmpPtr += sizeof(double);
    msgSize -= sizeof(double);

    if( msgSize < sizeof(msgRef.latHemi_) )
    {
        return false;
    }

    msgRef.latHemi_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.long_) )
    {
        return false;
    }

    memcpy(&(msgRef.long_), tmpPtr, sizeof(double));
    tmpPtr += sizeof(double);
    msgSize -= sizeof(double);

    if( msgSize < sizeof(msgRef.longHemi_) )
    {
        return false;
    }

    msgRef.longHemi_ = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    if( msgSize < sizeof(msgRef.speed_) )
    {
        return false;
    }

    memcpy(&(msgRef.speed_), tmpPtr, sizeof(double));
    tmpPtr += sizeof(double);
    msgSize -= sizeof(double);

    if( msgSize < sizeof(msgRef.course_) )
    {
        return false;
    }

    memcpy(&(msgRef.course_), tmpPtr, sizeof(double));
    tmpPtr += sizeof(double);
    msgSize -= sizeof(double);

    if( msgSize < sizeof(msgRef.magDeg) )
    {
        return false;
    }

    memcpy(&(msgRef.magDeg), tmpPtr, sizeof(double));
    tmpPtr += sizeof(double);
    msgSize -= sizeof(double);

    if( msgSize < sizeof(msgRef.dierction) )
    {
        return false;
    }

    msgRef.dierction = *tmpPtr++;
    msgSize -= sizeof(int8_t);

    return true;
}

bool build_MSG_GPSINFO_REQ(uint8_t* msgData, uint32_t& msgSize, MSG_GPSINFO_REQ& msgRef)
{
    if( msgData==NULL )
    {
        return false;
    }

    uint8_t* tmpPtr = msgData;
    uint32_t v32 = 0;
    memcpy(&v32, &(msgRef.btsId_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    *tmpPtr++ = msgRef.cpuUtil_;
    msgSize++;

    *tmpPtr++ = msgRef.memUtil_;
    msgSize++;

    *tmpPtr++ = msgRef.sdUtil_;
    msgSize++;

    *tmpPtr++ = msgRef.temp_;
    msgSize++;

    *tmpPtr++ = msgRef.powerStat_;
    msgSize++;

    *tmpPtr++ = msgRef.g3CardStat_;
    msgSize++;

    *tmpPtr++ = msgRef.wifiStat_;
    msgSize++;

    *tmpPtr++ = msgRef.paSata;
    msgSize++;

    *tmpPtr++ = msgRef.att_;
    msgSize++;

    *tmpPtr++ = msgRef.paOut_;
    msgSize++;

    memcpy(&v32, &(msgRef.uiYear_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(&v32, &(msgRef.uiMounth_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(&v32, &(msgRef.uiDayOfWeek), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(&v32, &(msgRef.uiDay_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(&v32, &(msgRef.uiHour_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(&v32, &(msgRef.uiMinute_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(&v32, &(msgRef.uiSecond_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    memcpy(&v32, &(msgRef.uiMilliseconds_), sizeof(v32));
    v32 = htonl(v32);
    memcpy(tmpPtr, &v32, sizeof(v32));
    tmpPtr += sizeof(v32);
    msgSize += sizeof(v32);

    *tmpPtr++ = msgRef.status_;
    msgSize++;

    memcpy(tmpPtr, &(msgRef.lat_), sizeof(double));
    tmpPtr += sizeof(double);
    msgSize += sizeof(double);

    *tmpPtr++ = msgRef.latHemi_;
    msgSize++;

    memcpy(tmpPtr, &(msgRef.long_), sizeof(double));
    tmpPtr += sizeof(double);
    msgSize += sizeof(double);

    *tmpPtr++ = msgRef.longHemi_;
    msgSize++;

    memcpy(tmpPtr, &(msgRef.speed_), sizeof(double));
    tmpPtr += sizeof(double);
    msgSize += sizeof(double);

    memcpy(tmpPtr, &(msgRef.course_), sizeof(double));
    tmpPtr += sizeof(double);
    msgSize += sizeof(double);

    memcpy(tmpPtr, &(msgRef.magDeg), sizeof(double));
    tmpPtr += sizeof(double);
    msgSize += sizeof(double);

    *tmpPtr++ = msgRef.dierction;
    msgSize++;

    return true;
}

static void handleTimeConfigRequest(const char* data, char* input, int *inLen)
{    
    const char * pFormat = "%Y%m%d%H%M%S";
    struct tm tmTime;
    time_t utcTime;
    strptime(data, pFormat, &tmTime);    
    utcTime = mktime(&tmTime);    
    cli_app_log(CLI_LOG_NOTICE, "set system time is data is %s %lu \n", data, utcTime);    
    msgConversion(CMD_CTRL,CT_TIME_SYNC, (I8 *)&utcTime, input, inLen);
}


void processMsgFromLidunXa(U16 dataType, uint8_t* msgPtr, uint32_t msgSize, char* input, ssize_t *inLen)
{
    WirelessConfig wConfig;
    memset(&wConfig, 0x0, sizeof(WirelessConfig));
    if( msgPtr==NULL )
    {
        return;
    }

    uint8_t* tmpPtr = msgPtr;

    uint16_t length = 0;
    uint16_t v16 = 0;
    memcpy(&v16, tmpPtr, sizeof(v16));
    length = ntohs(v16);
    tmpPtr += sizeof(v16);

    uint8_t msgType = *tmpPtr;
    tmpPtr++;
    
    uint32_t bodyLenth = msgSize-sizeof(length)-sizeof(msgType);

    switch(msgType)
    {
        case MSG_START_REQ_TYPE_CODE:
        {
            MSG_START_REQ inMsg;
            parse_MSG_START_REQ(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_START_ACK_TYPE_CODE:
        {
            MSG_START_ACK inMsg;
            parse_MSG_START_ACK(tmpPtr, bodyLenth, inMsg);
			if(inMsg.result_ == 0)
			{
                msgConversion(CMD_OTHERS, SEX_REG_SUCCESS, NULL, input, inLen);
			}
			else
			{
				msgConversion(CMD_OTHERS, SEX_UN_REG, NULL, input, inLen);			
			}

            printf("MSG_START_ACK_TYPE_CODE %d \n", MSG_START_ACK_TYPE_CODE);
			
			break;
        }
        case MSG_CONFIG_REPORT_REQ_TYPE_CODE:
        {
            MSG_CONFIG_REPORT_REQ inMsg;
            parse_MSG_CONFIG_REPORT_REQ(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_CONFIG_REPORT_ACK_TYPE_CODE:
        {
            MSG_CONFIG_REPORT_ACK inMsg;
            parse_MSG_CONFIG_REPORT_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_SET_DATETIME_REQ_TYPE_CODE:
        {
            MSG_SET_DATETIME_REQ inMsg;
            parse_MSG_SET_DATETIME_REQ(tmpPtr, bodyLenth, inMsg);

			string dateTime(inMsg.date_);
			handleTimeConfigRequest(dateTime.c_str(),input, inLen);
            break;
        }
        case MSG_SET_DATETIME_ACK_TYPE_CODE:
        {
            MSG_SET_DATETIME_ACK inMsg;
            parse_MSG_SET_DATETIME_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_SET_BTS_REQ_TYPE_CODE:
        {
            MSG_SET_BTS_REQ inMsg;
            parse_MSG_SET_BTS_REQ(tmpPtr, bodyLenth, inMsg);
			wConfig.capGsm.intvl = inMsg.lacUpdateCircle_ / 60;

			if(inMsg.frequency_ >= 0 && inMsg.frequency_ <= 95)
			{
			    wConfig.wirelessSupported[GSM_CELL_START] = 1;
			  //  wConfig.capGsm.enable = 1;
				wConfig.capGsm.gsm[0].bcc = inMsg.frequency_;
				wConfig.capGsm.gsm[0].band = 900;
				wConfig.capGsm.gsm[0].captime = inMsg.lacUpdateCircle_ ;
				wConfig.capGsm.gsm[0].PowerLevel = inMsg.power_;
			//	wConfig.capGsm.gsm[0].lac = inMsg.rac_;
			//	cmccLAC = inMsg.rac_;
			}
			else if(inMsg.frequency_ >= 96 && inMsg.frequency_ <= 124)
			{
			    wConfig.wirelessSupported[GSM_CELL_END] = 1;
			  //  wConfig.capGsm.enable = 1;
				wConfig.capGsm.gsm[1].bcc = inMsg.frequency_;
				wConfig.capGsm.gsm[1].band = 900;
				wConfig.capGsm.gsm[1].captime = inMsg.lacUpdateCircle_ ;
				wConfig.capGsm.gsm[1].PowerLevel = inMsg.power_;
			//	wConfig.capGsm.gsm[1].lac = inMsg.rac_;
			//	unicomLAC = inMsg.rac_;
			}
			else if(inMsg.frequency_ >= 512 && inMsg.frequency_ <= 636)
			{
			    wConfig.wirelessSupported[GSM_CELL_START] = 1;
			 //   wConfig.capGsm.enable = 1;
				wConfig.capGsm.gsm[0].bcc = inMsg.frequency_;
				wConfig.capGsm.gsm[0].band = 1800;
				wConfig.capGsm.gsm[0].captime = inMsg.lacUpdateCircle_;
				wConfig.capGsm.gsm[0].PowerLevel = inMsg.power_;
			//	wConfig.capGsm.gsm[0].lac = inMsg.rac_;
			//	cmccLAC = inMsg.rac_;				
			}
			else if(inMsg.frequency_ >= 637 && inMsg.frequency_ <= 736)
			{
			    wConfig.wirelessSupported[GSM_CELL_END] = 1;
			  //  wConfig.capGsm.enable = 1;
				wConfig.capGsm.gsm[1].bcc = inMsg.frequency_;
				wConfig.capGsm.gsm[1].band = 1800;
				wConfig.capGsm.gsm[1].captime = inMsg.lacUpdateCircle_ ;
				wConfig.capGsm.gsm[1].PowerLevel = inMsg.power_;
			//	wConfig.capGsm.gsm[1].lac = inMsg.rac_;
			//	unicomLAC = inMsg.rac_;				
			}
			else
			{
				cli_app_log(CLI_LOG_ERROR, "frequency not supported \n", inMsg.frequency_);   
				break;
			}
			
            msgConversion(CMD_CONFIG,CFG_WIRELESS, (I8 *)&wConfig, input, inLen);			
            break;
        }
        case MSG_SET_BTS_ACK_TYPE_CODE:
        {
            MSG_SET_BTS_ACK inMsg;
            parse_MSG_SET_BTS_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_USER_REPORT_REQ_TYPE_CODE:
        {
            MSG_USER_REPORT_REQ inMsg;
            parse_MSG_USER_REPORT_REQ(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_GET_HISTORY_REQ_TYPE_CODE:
        {
            MSG_GET_HISTORY_REQ inMsg;
            parse_MSG_GET_HISTORY_REQ(tmpPtr, bodyLenth, inMsg);
            SEARCHRECORD record = {0};
            record.seqNum = inMsg.sequenceNumber_;
            strncpy(record.date, inMsg.dateTime_, 8);
            msgConversion(CMD_SEARCH, SEARCH_ONE_RECORD, (I8 *)&record, input, inLen);	
            break;
        }
        case MSG_GET_HISTORY_ACK_TYPE_CODE:
        {
            MSG_GET_HISTORY_ACK inMsg;
            parse_MSG_GET_HISTORY_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_HEARTBEAT_REQ_TYPE_CODE:
        {
            MSG_HEARTBEAT_REQ inMsg;
            parse_MSG_HEARTBEAT_REQ(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_WARNING_REQ_TYPE_CODE:
        {
            MSG_WARNING_REQ inMsg;
            parse_MSG_WARNING_REQ(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_RESET_REQ_TYPE_CODE:
        {
            MSG_RESET_REQ inMsg;
            parse_MSG_RESET_REQ(tmpPtr, bodyLenth, inMsg);
            msgConversion(CMD_CTRL, CT_RESRART, NULL, input, inLen);
		    cli_app_log(CLI_LOG_NOTICE, "CT_RESTART\n");
            break;
        }
        case MSG_RESET_ACK_TYPE_CODE:
        {
            MSG_RESET_ACK inMsg;
            parse_MSG_RESET_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_SET_CONTROL_REQ_TYPE_CODE:
        {
            MSG_SET_CONTROL_REQ inMsg;
            parse_MSG_SET_CONTROL_REQ(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_SET_CONTROL_ACK_TYPE_CODE:
        {
            MSG_SET_CONTROL_ACK inMsg;
            parse_MSG_SET_CONTROL_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_CONTROL_OPTION_REQ_TYPE_CODE:
        {
            MSG_CONTROL_OPTION_REQ inMsg;
            parse_MSG_CONTROL_OPTION_REQ(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_CONTROL_OPTION_ACK_TYPE_CODE:
        {
            MSG_CONTROL_OPTION_ACK inMsg;
            parse_MSG_CONTROL_OPTION_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_RSSI_REPORT_REQ_TYPE_CODE:
        {
            MSG_RSSI_REPORT_REQ inMsg;
            parse_MSG_RSSI_REPORT_REQ(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_SMS_RECV_REQ_TYPE_CODE:
        {
            MSG_SMS_RECV_REQ inMsg;
            parse_MSG_SMS_RECV_REQ(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_CALL_RECV_REQ_TYPE_CODE:
        {
            MSG_CALL_RECV_REQ inMsg;
            parse_MSG_CALL_RECV_REQ(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_GET_VERSION_TYPE_CODE:
        {
            MSG_GET_VERSION inMsg;
            parse_MSG_GET_VERSION(tmpPtr, bodyLenth, inMsg);
            msgConversion(CMD_SEARCH,  SR_DEV_VERSION, NULL, input, inLen);
            break;
        }
        case MSG_GET_VERSION_ACK_TYPE_CODE:
        {
            MSG_GET_VERSION_ACK inMsg;
            parse_MSG_GET_VERSION_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_SEND_PACKET_TYPE_CODE:
        {
            MSG_SEND_PACKET inMsg;
            parse_MSG_SEND_PACKET(tmpPtr, bodyLenth, inMsg);
            ZHONGDUN_PACKET packet;
            memset(&packet, 0, sizeof(ZHONGDUN_PACKET));
            packet.len = inMsg.length_;
            packet.sn = inMsg.sn_;
            cli_app_log(CLI_LOG_NOTICE, "packet len is %d  %d sn %d\n", packet.len, inMsg.length_, packet.sn);
            if (packet.len > 237 || packet.len < 0)
            {
                memcpy(packet.data, inMsg.data_, 237);
            }
            else
                memcpy(packet.data, inMsg.data_, packet.len);
            memcpy(packet.crc, inMsg.crc_, 40);
            msgConversion(CMD_SEARCH,  SEND_UPGRADE_PACKET, (I8 *)&packet, input, inLen);
            break;
        }
        case MSG_SEND_PACKET_ACK_TYPE_CODE:
        {
            MSG_SEND_PACKET_ACK inMsg;
            parse_MSG_SEND_PACKET_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_DOWNLOAD_END_TYPE_CODE:
        {
            MSG_DOWNLOAD_END inMsg;
            parse_MSG_DOWNLOAD_END(tmpPtr, bodyLenth, inMsg);
            system("tar zxvf /mnt/usr/appfs/pkg/x86_pkg.tar.gz");
            msgConversion(CMD_SEARCH,  SEND_PACKET_END, NULL, input, inLen);
            break;
        }
        case MSG_GET_HISTORY_INFOMATION_REQ_TYPE_CODE:
        {
            MSG_GET_HISTORY_INFOMATION_REQ inMsg;
            parse_MSG_GET_HISTORY_INFOMATION_REQ(tmpPtr, bodyLenth, inMsg);
            msgConversion(CMD_SEARCH,  SR_50_DAY_RECORD, NULL, input, inLen);
            break;
        }
        case MSG_GET_HISTORY_INFOMATION_ACK_TYPE_CODE:
        {
            MSG_GET_HISTORY_INFOMATION_ACK inMsg;
            parse_MSG_GET_HISTORY_INFOMATION_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_SEND_RECORD_SELECT_TYPE_CODE:
        {
            MSG_SEND_RECORD_SELECT inMsg;
            parse_MSG_SEND_RECORD_SELECT(tmpPtr, bodyLenth, inMsg);
            msgConversion(CMD_SEARCH, SR_ONE_DAY_RECORD, inMsg.date_, input, inLen);	
            break;
        }
        case MSG_SEND_RECORD_SELECT_ACK_TYPE_CODE:
        {
            MSG_SEND_RECORD_SELECT_ACK inMsg;
            parse_MSG_SEND_RECORD_SELECT_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_SEND_RECORD_REQ_TYPE_CODE:
        {
            MSG_SEND_RECORD_REQ inMsg;
            parse_MSG_SEND_RECORD_REQ(tmpPtr, bodyLenth, inMsg);
            msgConversion(CMD_SEARCH, SR_RECORD_BY_SEQ, (I8 *)&inMsg.sn_, input, inLen);	
            break;
        }
        case MSG_SEND_RECORD_ACK_TYPE_CODE:
        {
            MSG_SEND_RECORD_ACK inMsg;
            parse_MSG_SEND_RECORD_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_SEND_RECORD_END_TYPE_CODE:
        {
            MSG_SEND_RECORD_END inMsg;
            parse_MSG_SEND_RECORD_END(tmpPtr, bodyLenth, inMsg);
            msgConversion(CMD_SEARCH,  SR_RECORD_END, NULL, input, inLen);
            break;
        }
        case MSG_GET_BTS_INFO_REQ_TYPE_CODE:
        {
            MSG_GET_BTS_INFO_REQ inMsg;
            parse_MSG_GET_BTS_INFO_REQ(tmpPtr, bodyLenth, inMsg);
            msgConversion(CMD_SEARCH,  SR_TOTAL_CFG, NULL, input, inLen);	

            break;
        }
        case MSG_GET_BTS_INFO_ACK_TYPE_CODE:
        {
            MSG_GET_BTS_INFO_ACK inMsg;
            parse_MSG_GET_BTS_INFO_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_GET_TIME_REQ_TYPE_CODE:
        {
            MSG_GET_TIME_REQ inMsg;
            parse_MSG_GET_TIME_REQ(tmpPtr, bodyLenth, inMsg);
            msgConversion(CMD_SEARCH, SR_X86_CURRENT_TIME, NULL, input, inLen);
            break;
        }
        case MSG_GET_TIME_ACK_TYPE_CODE:
        {
            MSG_GET_TIME_ACK inMsg;
            parse_MSG_GET_TIME_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_DELETE_USERDATA_REQ_TYPE_CODE:
        {
            MSG_DELETE_USERDATA_REQ inMsg;
            parse_MSG_DELETE_USERDATA_REQ(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_DELETE_USERDATA_ACK_TYPE_CODE:
        {
            MSG_DELETE_USERDATA_ACK inMsg;
            parse_MSG_DELETE_USERDATA_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_RF_POWER_REQ_TYPE_CODE:
        {

            I32     cellIdx = 0;
            if (dataType == IF_APP_EVENT_MNG_SERVER)
            {
                cellIdx = 8;
            }
            else
            {
                cellIdx = 9;
            }
            MSG_RF_POWER_REQ inMsg;
            parse_MSG_RF_POWER_REQ(tmpPtr, bodyLenth, inMsg);
			if(inMsg.onOfOff_ == 0)
	            msgConversion(CMD_CTRL, CT_BBU_PW_ON, (I8 *)&cellIdx, input, inLen);
			else
	            msgConversion(CMD_CTRL, CT_BBU_PW_OFF, (I8 *)&cellIdx, input, inLen);
            break;
        }
        case MSG_RF_POWER_ACK_TYPE_CODE:
        {
            MSG_RF_POWER_ACK inMsg;
            parse_MSG_RF_POWER_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_SCAN_REQ_TYPE_CODE:
        {
            MSG_SCAN_REQ inMsg;
            parse_MSG_SCAN_REQ(tmpPtr, bodyLenth, inMsg);
            sockType = dataType;
            msgConversion(CMD_CTRL, CT_SNIFFER, NULL, input, inLen);
            break;
        }
        case MSG_SCAN_ACK_TYPE_CODE:
        {
            MSG_SCAN_ACK inMsg;
            parse_MSG_SCAN_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_XML_SCAN_ACK_TYPE_CODE:
        {
            MSG_XML_SCAN_ACK inMsg;
            parse_MSG_XML_SCAN_ACK(tmpPtr, bodyLenth, inMsg);
            break;
        }
        case MSG_GPSINFO_REQ_TYPE_CODE:
        {
            MSG_GPSINFO_REQ inMsg;
            parse_MSG_GPSINFO_REQ(tmpPtr, bodyLenth, inMsg);
            break;
        }
        default:
        {
            cli_app_log(CLI_LOG_DEFAULT, "Unkown message type 0x%x", msgType);
            break;
        }
    }

}


static void handleCellConfigMsg(I32 type, I32 code, I32 result, I8 *indata, I8 *outdata, I32* len)
{
	uint16_t *length = (uint16_t*)outdata;
	uint8_t  *msgType = (uint8_t*)(length + 1);
	uint32_t msgLen = 0;
	uint8_t   *msgPtr = (uint8_t*)(outdata + 3);

	MSG_SET_BTS_ACK setBtsAck;

	switch(code)
	{
	   case CFG_WIRELESS:
	   	   setBtsAck.result_ = 1;
	   	   if(result != RST_FAILED) setBtsAck.result_ = 0;
	   	   build_MSG_SET_BTS_ACK((uint8_t *)msgPtr, msgLen, setBtsAck);
		   *len = msgLen + 3;
		   *length = htons(*len);
		   *msgType = MSG_SET_BTS_ACK_TYPE_CODE;		   	
		   break;
	   case CFG_PA_GAIN:
       case CFG_BLACKLIST_ADD:
       case CFG_BLACKLIST_DEL:
	   case CFG_UPDATA_TAC_CYCLE:
       case CFG_DET_INTVL:
	   case CFG_AIR_SNIFFER:
       case CFG_FTP_INFO:
	   default:
           *len = 0;
		   cli_app_log(CLI_LOG_DEFAULT, "handleCellConfigMsg cmdcode %04x not supported\n", code); 		   
		   break;
	}
}

static void handleCellControlMsg(I32 type, I32 code, I32 result, I8 *indata, I8 *outdata, I32* len)
{
	MSG_RESET_ACK resetAck;
	MSG_SET_DATETIME_ACK dateAck;
	MSG_RF_POWER_ACK rfAck;
	uint16_t *length = (uint16_t*)outdata;
	uint8_t  *msgType = (uint8_t*)(length + 1);
	uint32_t msgLen = 0;
	uint8_t   *msgPtr = (uint8_t*)(outdata + 3);

	switch(code)
	{
	   case CT_RESRART:
	   	   build_MSG_RESET_ACK((uint8_t *)msgPtr, msgLen, resetAck);
		   *len = msgLen + 3;
		   *length = htons(*len);
		   *msgType = MSG_RESET_ACK_TYPE_CODE;
		   break;
	   case CT_TIME_SYNC:
	   	   dateAck.result_ = 1;
	   	   if (result != RST_FAILED) dateAck.result_ = 0;	   	
	   	   build_MSG_SET_DATETIME_ACK((uint8_t *)msgPtr, msgLen, dateAck);
		   *len = msgLen + 3;
		   *length = htons(*len);

		   *msgType = MSG_SET_DATETIME_ACK_TYPE_CODE;
		   break;
       case CT_WIRELISS_ON:
       case CT_WIRELISS_OFF:	   	
	   	   build_MSG_RF_POWER_ACK((uint8_t *)msgPtr, msgLen, rfAck);	
		   *len = msgLen + 3;
		   *length = htons(*len);
		   *msgType = MSG_RF_POWER_ACK_TYPE_CODE;		   
		   
	   	   break;
       case CT_BBU_PW_OFF:
       case CT_BBU_PW_ON:
       	   	build_MSG_RF_POWER_ACK((uint8_t *)msgPtr, msgLen, rfAck);	
		   *len = msgLen + 3;
		   *length = htons(*len);
		   *msgType = MSG_RF_POWER_ACK_TYPE_CODE;		   
	   	   break;
 	   case CT_SNIFFER:
	   default:
           *len = 0;
		   cli_app_log(CLI_LOG_DEFAULT, "handleCellControlMsg cmdcode %04x not supported\n", code);	   
		   break;
	}
}

static void handleCellSearchMsg(U16 dataType, I32 type, I32 code, I32 result, I8 *indata, I8 *outdata, I32* len)
{
	WirelessConfig wconfig;
	MSG_GET_BTS_INFO_ACK btsInfo;
	uint16_t *length = (uint16_t*)outdata;
	uint8_t  *msgType = (uint8_t*)(length + 1);
	uint32_t msgLen = 0;
	uint8_t   *msgPtr = (uint8_t*)(outdata + 3);	
    uint32_t  payloadsize = 0;

    switch(code)
    {
        /*
        case SR_TOTAL_CFG:
	        memset(&wconfig, 0, sizeof(wconfig));	
	        conversionConfig(&wconfig);	
			btsInfo.arfcn_ = wconfig.capGsm.gsm[0].bcc;
			btsInfo.power_ = wconfig.capGsm.gsm[0].lowatt;
			btsInfo.luc_ = wconfig.capGsm.gsm[0].captime * 60;
			btsInfo.rac_ = wconfig.capGsm.gsm[0].lac;			
			build_MSG_GET_BTS_INFO_ACK((uint8_t *)msgPtr, msgLen, btsInfo);
		    payloadsize = msgLen + 3;
		    *length = htons(payloadsize);
            *msgType = MSG_GET_BTS_INFO_ACK_TYPE_CODE;
			
            msgPtr += msgLen;
			length = (uint16_t *)msgPtr;
			msgType = msgPtr + 2;
            msgPtr += 3;

            msgLen = 0;
			btsInfo.arfcn_ = wconfig.capGsm.gsm[1].bcc;
			btsInfo.power_ = wconfig.capGsm.gsm[1].lowatt;
			btsInfo.luc_ = wconfig.capGsm.gsm[1].captime * 60;
			btsInfo.rac_ = wconfig.capGsm.gsm[1].lac;			
			build_MSG_GET_BTS_INFO_ACK((uint8_t *)msgPtr, msgLen, btsInfo);
		    payloadsize = payloadsize + msgLen + 3;
		    *length = htons(msgLen + 3);
		    *msgType = MSG_GET_BTS_INFO_ACK_TYPE_CODE;

			*len = payloadsize;
		   
            break;*/
        case SR_BLACKLIST:
            break;

        case SR_DEV_VERSION:
            {
                MSG_GET_VERSION_ACK versionReport;
                versionReport.version_[0] = 2;
                versionReport.version_[1] = 0;
                strcpy(versionReport.date_ ,"2018-01-10 9:40:20");
                build_MSG_GET_VERSION_ACK((uint8_t *)msgPtr, msgLen, versionReport);
                *len = msgLen + 3;
                *length = htons(*len);
                *msgType = MSG_GET_VERSION_ACK_TYPE_CODE;
            }
            break;
        case SR_X86_CURRENT_TIME:
            {
                MSG_GET_TIME_ACK timeReport;
                time_t rawtime;
                time(&rawtime);
                timeReport.timeStame_ = (U32) rawtime; 
                build_MSG_GET_TIME_ACK((uint8_t *)msgPtr, msgLen, timeReport);
                *len = msgLen + 3;
                *length = htons(*len);
                *msgType = MSG_GET_TIME_ACK_TYPE_CODE;
            }
            break;

        case SR_TOTAL_CFG:
            {
                MSG_GET_BTS_INFO_ACK btsReport;
                if (dataType == IF_APP_EVENT_MNG_SERVER)
                {
                    btsReport.arfcn_ = atoi(dev_conf.capGsm.gsm1.bcc);
                    btsReport.power_ = atoi(dev_conf.capGsm.gsm1.PowerLevel);
                    btsReport.luc_ = atoi(dev_conf.capGsm.gsm1.captime);
                    btsReport.rac_ = -110;
                }
                else
                {
                    btsReport.arfcn_ = atoi(dev_conf.capGsm.gsm2.bcc);
                    btsReport.power_ = atoi(dev_conf.capGsm.gsm2.PowerLevel);
                    btsReport.luc_ = atoi(dev_conf.capGsm.gsm2.captime);
                    btsReport.rac_ = -110;
                }
                build_MSG_GET_BTS_INFO_ACK((uint8_t *)msgPtr, msgLen, btsReport);
                *len = msgLen + 3;
                *length = htons(*len);
                *msgType = MSG_GET_BTS_INFO_ACK_TYPE_CODE;
                
            }
            break;

        default:
            *len = 0;
            cli_app_log(CLI_LOG_DEFAULT, "handleCellSearchMsg cmdcode %04x not supported\n", code); 		   
            break;
    }
}

static void handleOtherMsg(U16 dataType, I32 type, I32 code, I32 result, I8 *indata, I8 *outdata, I32* len)
{
	RegisterMsg *registeMsg= NULL;
    gsmSnifferResult* sniff= NULL;
	MSG_HEARTBEAT_REQ heartBeat;
	uint16_t   version = 0x0048;
	const char *data = "20171217121212";
	MSG_START_REQ startReq;			

	uint16_t *length = (uint16_t*)outdata;
	uint8_t  *msgType = (uint8_t*)(length + 1);
	uint32_t msgLen = 0; 
	uint8_t  *msgPtr = (uint8_t*)(outdata + 3);

	switch(code)
	{
	 	case HT_SRV_STATUS_OK:
            if (dataType == IF_APP_EVENT_MNG_SERVER)
			    heartBeat.btsid_ = equipmentID;
            else
                heartBeat.btsid_ = equipmentID1;
			build_MSG_HEARTBEAT_REQ((uint8_t *)msgPtr, msgLen, heartBeat);
		    *len = msgLen + 3;
		    *length = htons(*len);
		    *msgType = MSG_HEARTBEAT_REQ_TYPE_CODE;
		   
		 	break;
	 	case SEX_REG_SUCCESS:
		 	registeMsg=(RegisterMsg *)indata;
            if (dataType == IF_APP_EVENT_MNG_SERVER)
            {
                equipmentID = atoi(dev_conf.capGsm.gsm1.EquipmentId);
                startReq.equipmentId_ = equipmentID;

            }
            else
            {
                equipmentID1 =  atoi(dev_conf.capGsm.gsm2.EquipmentId);
                startReq.equipmentId_ = equipmentID1;
            }
			startReq.version_ = atof(devVersion.version) * 100;
			memcpy(startReq.date_, devVersion.buildDate, strlen(devVersion.buildDate));
            memcpy(startReq.date_ + strlen(devVersion.buildDate), devVersion.buildTime, strlen(devVersion.buildTime));

	        build_MSG_START_REQ((uint8_t *)msgPtr, msgLen, startReq);
			*len = msgLen + 3;
		    *length = htons(*len);
		    *msgType = MSG_START_REQ_TYPE_CODE;
			
		 	break;

        case DEVICE_REQ_BTS_INFO:
            {
                MSG_CONFIG_REPORT_REQ configReport;
                if (dataType == IF_APP_EVENT_MNG_SERVER)
                    configReport.equipmentId_ = equipmentID;
                else
                    configReport.equipmentId_ = equipmentID1;
                configReport.frequency_ = atoi(dev_conf.capGsm.gsm1.bcc);
                configReport.power_ = atoi(dev_conf.capGsm.gsm1.lowatt);
                build_MSG_START_REQ((uint8_t *)msgPtr, msgLen, startReq);
                *len = msgLen + 3;
                *length = htons(*len);
                *msgType = MSG_CONFIG_REPORT_REQ_TYPE_CODE;
            }
            break;

        case SNF_RESULT:
            {
                sniff =  (gsmSnifferResult* )indata;
                MSG_SCAN_ACK msgRef;
                int32_t erafcnList[6] = {0, 0, 0, 0, 0, 0};
                int32_t listNum = 0;
                
                if (dataType == IF_APP_EVENT_MNG_SERVER)
                {
                    for (uint32_t i = 0; i < sniff->cellNum; i++)
                    {
                        if (strcmp(sniff->cell[i].wPlmnid, "46000") == 0)
                        {
                            erafcnList[listNum++] = sniff->cell[i].wARFCN;
                            if (listNum > 5)
                            {
                                break;
                            }

                        }
                    }
                    msgRef.equipmentId_ = equipmentID;
                }
                else
                {
                    for (uint32_t i = 0; i < sniff->cellNum; i++)
                    {
                        if (strcmp(sniff->cell[i].wPlmnid, "46001") == 0)
                        {
                            erafcnList[listNum++] = sniff->cell[i].wARFCN;
                            if (listNum > 5)
                            {
                                break;
                            }
                        }
                    }
                    msgRef.equipmentId_ = equipmentID1;
                }
                
                msgRef.neighbourFrequencyTotle_ = listNum;
                if (listNum > 0)
                {
                    msgRef.bestFrequencyTotle_ = 1;
                }
                for (int i = 0; i < listNum;  i++)
                {
                    msgRef.neighInfo_[i].neighbourFrequency_ = erafcnList[i]; 
                }
                cli_app_log(CLI_LOG_INFO, "snf result num is %d list %d \n", sniff->cellNum, listNum);
                msgRef.bestFreqInfo_[0].bestFrequency_ = erafcnList[0];
                build_MSG_SCAN_ACK((uint8_t *)msgPtr, msgLen, msgRef);
                *len = msgLen + 3;
                *length = htons(*len);
                *msgType = MSG_SCAN_ACK_TYPE_CODE;
                std::stringstream ss;
                ss << "len[" << *length << "]: ";
                for(uint32_t i=0; i<*len; i++)
                {
                    ss << "0x" << std::hex << static_cast<uint32_t>(outdata[i]) << " ";
                }
                cli_app_log(CLI_LOG_INFO, "zhongdun sniffer result %s \n", ss.str().c_str());

            }
            break;
		default:
			cli_app_log(CLI_LOG_DEFAULT, "unsupported command code failed %d\n", code);
            *len = 0;
			break;
     }
}


void sendMsgToZhongdunXa(U16 dateType, const char* buf, ssize_t len, char* output, ssize_t* outlen)
{
    if(dateType == IF_APP_EVENT_IMSI_DATA || dateType == IF_APP_EVENT_IMSI_MNG)
    {
    	int totalPayloadSize = 0;
		char *dataPtr = output;
	
		ZHONGDUNLIST *recordPtr = (ZHONGDUNLIST *)buf;
		if ( recordPtr != NULL )
		{
			cli_app_log(CLI_LOG_INFO, "send recordlist is %d \n", recordPtr->num);
			for (int i = 0; i < recordPtr->num ; i++)
			{
			    uint16_t *length = (uint16_t*)dataPtr;
             	uint8_t  *msgType = (uint8_t*)(length + 1);
                dataPtr += 3;

			    uint32_t payloadSize = 0;

			    MSG_USER_REPORT_REQ ueReport;
				ueReport.sequenceNumber_ = recordPtr->zhongdunList[i].seqNum;   
                if (dateType == IF_APP_EVENT_IMSI_MNG)             
				    ueReport.equipmentID_ = equipmentID;
                else
                    ueReport.equipmentID_ = equipmentID1;
				ueReport.lac_ = recordPtr->zhongdunList[i].lac;
				ueReport.timestamp_ = recordPtr->zhongdunList[i].uptime;
				encBCD(recordPtr->zhongdunList[i].imsi, 8, ueReport.imsi_);				
				encBCD(recordPtr->zhongdunList[i].imei, 8, ueReport.imei_);
				build_MSG_USER_REPORT_REQ((uint8_t *)dataPtr, payloadSize, ueReport);

				*length = htons(payloadSize + 3);
				*msgType = MSG_USER_REPORT_REQ_TYPE_CODE;

				dataPtr += payloadSize;
				totalPayloadSize += payloadSize + 3;
                cli_app_log(CLI_LOG_DEFAULT, "lac = %d, imsi %s\n", recordPtr->zhongdunList[i].lac, recordPtr->zhongdunList[i].imsi);
			}

			*outlen = totalPayloadSize;
		}
		else
		{
            *outlen = 0;
			cli_app_log(CLI_LOG_ERROR, "input data is empty\n");
		}
		
		return;
    }

	cli_app_log(CLI_LOG_DEFAULT, "Sending Json message: %s\n", buf);
    
    //todo, decoder json string
    I32 cmdType;
	I32 cmdCode;
    I32 result;
	I8  outdata[65535];
    *outlen = 0;
    if(rspMsgConversion(&cmdType, &cmdCode, &result, (I8 *)buf, outdata, len))
    {
        switch(cmdType)
        {
            case CMD_OTHERS:
				handleOtherMsg(dateType, cmdType, cmdCode, result, outdata, output, outlen);
 				break;
			case CMD_CONFIG:
				handleCellConfigMsg(cmdType, cmdCode, result, outdata, output, outlen);
				break;
			case CMD_CTRL:
				handleCellControlMsg(cmdType, cmdCode, result, outdata, output, outlen);				
				break;
			case CMD_SEARCH:
				handleCellSearchMsg(dateType, cmdType, cmdCode, result, outdata, output, outlen);
				break;
			default:
				cli_app_log(CLI_LOG_ERROR, "not support command type: %s\n", cmdType);
				break;
        }
    }

}

void handleMsgFromZhongdunXa(U16 dataType, const char * inMsgBuf, ssize_t inMsgSize, char* input, ssize_t *inLen)
{
    if( inMsgBuf==NULL )
    {
        return;
    }

    //debug_dump_buffer(inMsgSize, inMsgBuf);
    recvZhongdunXaMsgBuf.append(inMsgBuf, inMsgSize);

    if( currentZhongdunXaMsgSize == 0 )
    {
        if( recvZhongdunXaMsgBuf.length() >= 3)
        {
            uint16_t len = 0;
            memcpy(&len, recvZhongdunXaMsgBuf.c_str(), sizeof(uint16_t));
            currentZhongdunXaMsgSize = ntohs(len);
        }
        else
        {
            return;
        }
    }

    while( recvZhongdunXaMsgBuf.length() >= currentZhongdunXaMsgSize )
    {
        std::string tmpBuf;
        tmpBuf.assign(recvZhongdunXaMsgBuf.c_str(), currentZhongdunXaMsgSize);
        recvZhongdunXaMsgBuf.erase(0, currentZhongdunXaMsgSize);
        
        const char* data =tmpBuf.c_str();

        cli_app_log(CLI_LOG_DEFAULT, "msg length %d \n", currentZhongdunXaMsgSize);

        processMsgFromLidunXa(dataType, (uint8_t *)data, currentZhongdunXaMsgSize, input, inLen);

        currentZhongdunXaMsgSize = 0;
        if( recvZhongdunXaMsgBuf.empty() )
        {
            break;
        }

        if( recvZhongdunXaMsgBuf.length()< 3)
        {
            break;
        }

        uint16_t len = 0;
        memcpy(&len, recvZhongdunXaMsgBuf.c_str(), sizeof(uint16_t));
        currentZhongdunXaMsgSize = ntohs(len);       
    }
}

I32 packSearchOneRecord(bool isMobile, I8 *buf, ZHONGDUNDATA data, I32 seqNum)
{
    MSG_GET_HISTORY_ACK msgRef;
    uint16_t *length = (uint16_t*)buf;
	uint8_t  *msgType = (uint8_t*)(length + 1);
	uint32_t msgLen = 0;
	uint8_t   *msgPtr = (uint8_t*)(buf + 3);
    I32 len = 0;
    if (data.seqNum == 0)
    {
        msgRef.result_ = 1;
        msgRef.sequenceNumber_ = seqNum;
        msgRef.timestamp_ = 0;
        encBCD("000000000000000", 8, msgRef.imsi_);
        encBCD("000000000000000", 8, msgRef.imei_);
        msgRef.lac_ = 0;
        if (isMobile)
            msgRef.equipmentID_ = equipmentID;
        else
            msgRef.equipmentID_ = equipmentID1;
    }
    else
    {
        msgRef.result_ = 0;
        msgRef.sequenceNumber_ = seqNum;
        msgRef.timestamp_ = data.uptime;
        encBCD(data.imsi, 8, msgRef.imsi_);
        encBCD(data.imei, 8, msgRef.imei_);
        msgRef.lac_ = data.lac;
        if (isMobile)
            msgRef.equipmentID_ = equipmentID;
        else
            msgRef.equipmentID_ = equipmentID1;

    }
    build_MSG_GET_HISTORY_ACK((uint8_t*)msgPtr, msgLen, msgRef);

    len = msgLen + 3;
	*length = htons(len);
	*msgType = MSG_GET_HISTORY_ACK_TYPE_CODE;
    return len;
}

I32 packHistoryInformationAck(I8 *buf, ZHONGDUN_DB_LIST list)
{
    MSG_GET_HISTORY_ACK msgRef;
    uint16_t *length = (uint16_t*)buf;
	uint8_t  *msgType = (uint8_t*)(length + 1);
	uint32_t msgLen = 0;
	uint8_t   *msgPtr = (uint8_t*)(buf + 3);
    I32 len = 0;
    MSG_GET_HISTORY_INFOMATION_ACK listAck;
    for (int8_t i = 0; i < 50; i++)
    {
        
        listAck.hisInfoRecords_.push_back(HISTORY_INFO_RECORD(list.record[i].date, list.record[i].seqNum));
    }
    build_MSG_GET_HISTORY_INFOMATION_ACK((uint8_t*)msgPtr, msgLen, listAck);
    len = msgLen + 3;
	*length = htons(len);
	*msgType = MSG_GET_HISTORY_INFOMATION_ACK_TYPE_CODE;
    return len;

    
}

I32 packOneDayRecordAck(I8 *buf, I32 fileSize)
{
    MSG_GET_HISTORY_ACK msgRef;
    uint16_t *length = (uint16_t*)buf;
	uint8_t  *msgType = (uint8_t*)(length + 1);
	uint32_t msgLen = 0;
	uint8_t   *msgPtr = (uint8_t*)(buf + 3);
    I32 len = 0;
    MSG_SEND_RECORD_SELECT_ACK sendAck;
    if (fileSize == 0)
    {
        sendAck.result_ = 1;
    }
    else
    {
        sendAck.result_ = 0;
    }
    sendAck.fileSize_ = fileSize;
    build_MSG_SEND_RECORD_SELECT_ACK((uint8_t*)msgPtr, msgLen, sendAck);
    len = msgLen + 3;
	*length = htons(len);
	*msgType = MSG_SEND_RECORD_SELECT_ACK_TYPE_CODE ;
    return len;
    
}

I32 packRecordBySn(bool isMobile, ZHONGDUNLIST *list, U16 Sn, I8 *buf)
{
    MSG_SEND_RECORD_ACK msgRef;
    uint16_t *length = (uint16_t*)buf;
	uint8_t  *msgType = (uint8_t*)(length + 1);
	uint32_t msgLen = 0;
	uint8_t   *msgPtr = (uint8_t*)(buf + 3);
    I32 len = 0, i = 0;
    SendRecord *record;
    I8 *data = msgRef.data_;
    I8 *data1 = msgRef.data_;
    for ( i= 0; i < list->num; i++)
    {
        record = (SendRecord *)data;
        record->seqNum = list->zhongdunList[i].seqNum;
        record->uptime = list->zhongdunList[i].uptime;
        record->lac = list->zhongdunList[i].lac;
        encBCD(list->zhongdunList[i].imsi, 8, record->imsi);
        encBCD(list->zhongdunList[i].imei, 8, record->imei);

        if (isMobile)
            record->equipmentId = equipmentID;
        else
            record->equipmentId  = equipmentID1;
        data += sizeof(SendRecord);
        cli_app_log(CLI_LOG_INFO, "pack num is %d \n", i);
    }
    I8 *CRC = msgRef.crc_;
    for(int t=0; t<20; t++)  
    {
        U16 crc16=crc_16(data1+t*170, 170);
        CRC[2*t]=(char)crc16;
        CRC[2*t+1]=crc16>>8;
    }
    cli_app_log(CLI_LOG_INFO, "crc end ------------\n");
    build_MSG_SEND_RECORD_ACK((uint8_t*)msgPtr, msgLen, msgRef);

    len = msgLen + 3;
	*length = htons(len);
	*msgType = MSG_SEND_RECORD_ACK_TYPE_CODE;
    return len;
    
}

I32 packsuccssAck(I8 *buf)
{
    MSG_DELETE_USERDATA_ACK  msgRef;
    uint16_t *length = (uint16_t*)buf;
	uint8_t  *msgType = (uint8_t*)(length + 1);
	uint32_t msgLen = 0;
	uint8_t   *msgPtr = (uint8_t*)(buf + 3);
    I32 len = 0;
    msgRef.result_ = 0;
    build_MSG_DELETE_USERDATA_ACK((uint8_t*)msgPtr, msgLen, msgRef);
    len = msgLen + 3;
	*length = htons(len);
	*msgType = MSG_DELETE_USERDATA_ACK_TYPE_CODE;
    return len;


}

I32 packUpgradeRsp(bool isSuccess, I8 *buf)
{
    MSG_SEND_PACKET_ACK  msgRef;
    uint16_t *length = (uint16_t*)buf;
	uint8_t  *msgType = (uint8_t*)(length + 1);
	uint32_t msgLen = 0;
	uint8_t   *msgPtr = (uint8_t*)(buf + 3);
    I32 len = 0;
    if (isSuccess)
    {
        msgRef.result_ = 0;
    }
    else
    {
        msgRef.result_ = 1;
    }
    build_MSG_SEND_PACKET_ACK((uint8_t*)msgPtr, msgLen, msgRef);
    len = msgLen + 3;
	*length = htons(len);
	*msgType = MSG_SEND_PACKET_ACK_TYPE_CODE;
    return len;

}

I32 packUpgradePacket(ZHONGDUN_PACKET packet, I8 *buf)
{
    if (!startUpgrade)
    {
        DIR *pAppfs = NULL, *pPkg = NULL;
        pAppfs = opendir(x86Appfs);
        if (pAppfs == NULL)
        {
            if (mkdir(x86Appfs, 0777) != 0)
            {
                cli_app_log(CLI_LOG_WARNING, "create dir %s failed\n", x86Appfs);
                return  packUpgradeRsp(false, buf);
            }
        }
        else
        {
            closedir(pAppfs);
        }
        pPkg = opendir(x86pkg);
        if (pPkg == NULL)
        {
            if (mkdir(x86pkg, 0777) != 0)
            {
                cli_app_log(CLI_LOG_WARNING, "create dir %s failed \n", x86pkg);
                return  packUpgradeRsp(false, buf);
            }
        }
        else
        {
            closedir(pPkg);
            system("rm -rf /mnt/usr/appfs/pkg/*");
        }
        startUpgrade = true;
    }

    FILE *fd = NULL;
    fd = fopen("/mnt/usr/appfs/pkg/x86_pkg.tar.gz", "a");
    if (fd == NULL)
    {
        cli_app_log(CLI_LOG_WARNING, "open /mnt/usr/appfs/pkg/x86_pkg.tar.gz failed \n");
        return  packUpgradeRsp(false, buf);
    }
    if (fwrite(packet.data, packet.len, 1, fd) != 1)
    {
        cli_app_log(CLI_LOG_WARNING, "write /mnt/usr/appfs/pkg/x86_pkg.tar.gz failed \n");
        return  packUpgradeRsp(false, buf);
    }
    fclose(fd);
    return  packUpgradeRsp(true, buf);

}

I32 cleanZhongdunState()
{
    startUpgrade = false;
}
